package com.lyf.scm.admin.api.controller;

import cn.hutool.core.date.DateUtil;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.service.PermissionService;
import com.lyf.scm.admin.domain.service.SalesPlanOtherService;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.enums.SalesPlanTypeEnum;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.model.ExcelExportDataDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import com.rome.arch.util.controller.RomeController;
import com.rome.user.common.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author: huangtao
 * @Description:
 * @Date: create in 2020/7/31
 */
@Slf4j
@Api(tags = "其他渠道计划")
@RequestMapping("/api/v1/salesPlanOther")
@RomeController
public class SalesPlanOtherController {
    @Resource
    private SalesPlanOtherService salesPlanOtherService;

    @Resource
    private ProxySelectorParamsDTO proxySelectorParamsDTO;
    @Resource
    private PermissionService permissionService;

    @PostMapping("/listWeek")
    @ApiOperation(value = "按条件查询其他渠道计划(周)")
    public Response<PlanResponseDTO<SalesPlanOtherInfoDTO>> listWeekBySelective(@RequestBody SalesPlanOtherDTO salesPlanOther) {
        return Response.builderSuccess(salesPlanOtherService.listWeekBySelective(salesPlanOther));
    }

    @PostMapping("/eChartsData")
    @ApiOperation(value = "图表数据接口")
    public Response<PlanResponseDTO<SalesPlanOtherInfoDTO>> eChartsData(@RequestBody SalesPlanOtherDTO salesPlanOther) {
        return Response.builderSuccess(salesPlanOtherService.eChartsData(salesPlanOther));
    }

    @PostMapping("/importVerification")
    @ApiOperation(value = "导入计划类型")
    public Response<Integer> importPlanType(@RequestBody MultipartFile file, @RequestParam String type) {
        return Response.builderSuccess(salesPlanOtherService.importPlanType(file, type));
    }

    @PostMapping("/getOtherPlanLevel")
    @ApiOperation(value = "获取其他渠道计划当前用户级别")
    public Response<Integer> getOtherPlanLevel() {
        return Response.builderSuccess(salesPlanOtherService.getOtherPlanLevel());
    }

    @PutMapping("/confirm")
    @ApiOperation("修改确认量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unitType", value = "单位类型", required = true, paramType = "query", dataType = "long"),
    })
    public Response<Boolean> confirm(@RequestBody List<SalesPlanOtherDTO> all, @RequestParam Long unitType) {
        return Response.builderSuccess(salesPlanOtherService.confirm(all, unitType));
    }


    @PostMapping("/downLoadTemplate")
    @ApiOperation("下载模板")
    public void downLoadTemplate(@RequestParam String type, @RequestParam String date, HttpServletResponse httpServletResponse) {
        try {
            //增加date参数为了测试周滚动下载模板
            if ("".equals(date)) {
                date = DateUtil.formatDate(new Date());
            }
            //获得feign响应流对象
            feign.Response response = salesPlanOtherService.downLoadTemplate(type,date);
            //如果返回普通json对象，处理异常响应
            if (ExcelExportUtil.validJsonResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                ExcelExportUtil.handleProviderErrorResponse(response, httpServletResponse);
            }
            //如果返回excel对象，处理正确响应
            if (ExcelExportUtil.validExcelResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                String sourceFileName = "";
                if (SalesPlanTypeEnum.PLAN_ELEVEN.getType().equals(type)) {
                    sourceFileName = "销售计划(加盟)导入模板";
                } else if (SalesPlanTypeEnum.PLAN_TWELVE.getType().equals(type)) {
                    sourceFileName = "销售计划(APP)导入模板";
                } else if (SalesPlanTypeEnum.PLAN_THIRTEEN.getType().equals(type)) {
                    sourceFileName = "销售计划(经销中心)导入模板";
                } else if (SalesPlanTypeEnum.PLAN_FOURTEEN.getType().equals(type)) {
                    sourceFileName = "销售计划(云商)导入模板";
                } else if (SalesPlanTypeEnum.PLAN_FIFTEEN.getType().equals(type)) {
                    sourceFileName = "新开店计划导入模板";
                } else if (SalesPlanTypeEnum.PLAN_SIXTEEN.getType().equals(type)) {
                    sourceFileName = "新品铺货计划导入模板";
                } else if (SalesPlanTypeEnum.PLAN_SEVENTEEN.getType().equals(type)) {
                    sourceFileName = "销售计划(直营)导入模板";
                }
                ExcelExportUtil.handleProviderRightResponse(response, httpServletResponse, sourceFileName);
            }
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }

    @PostMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "导出类型-1导出当前页-2导出选中数据-3导出所有数据", required = true, paramType = "query")
    })
    @ApiOperation(value = "导出")
    public Response<String> export(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            OperationLogRecordDTO recordDto = salesPlanOtherService.saveOperatorLog(excelExportDTO, type);
            List<String> channelCodes = permissionService.getPermissionChannelList();
//            List<String> channelCodes = Arrays.asList("C00001");
            if (null == channelCodes) {
                throw new RomeException(ResponseMsg.FAIL_6009.getCode(), ResponseMsg.FAIL_6009.getMsg());
            }
            Integer level = permissionService.getPermissionLevel();
//            Integer level = 2;
            if (null == level) {
                //level = 1;
                throw new RomeException(ResponseMsg.FAIL_2004.getCode(), ResponseMsg.FAIL_2004.getMsg());
            }
            salesPlanOtherService.createExportFile(recordDto, excelExportDTO, type, channelCodes, level, UserContext.getUserId());
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
        return Response.builderSuccess("导出任务处理中，请至任务列表查看！");
        /*try {
            //获得feign响应流对象
            feign.Response response = salesPlanOtherService.export(excelExportDTO, type);
            //如果返回普通json对象，处理异常响应
            if (ExcelExportUtil.validJsonResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                ExcelExportUtil.handleProviderErrorResponse(response, httpServletResponse);
            }
            //如果返回excel对象，处理正确响应
            if (ExcelExportUtil.validExcelResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                String sourceFileName = "其他渠道计划";
                ExcelExportUtil.handleProviderRightResponse(response, httpServletResponse, sourceFileName);
            }
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }*/
    }


    @GetMapping("/getImportTime")
    @ApiOperation(value = "获取springcloud配置的导入时间")
    public Response<String> getImportTime(){
        return Response.builderSuccess(proxySelectorParamsDTO.getImportTime());
    }

    @GetMapping("/getConfirmTime")
    @ApiOperation(value = "获取springcloud配置确认时间")
    public Response<String> getConfirmTime(){
        return Response.builderSuccess(proxySelectorParamsDTO.getConfirmTime());
    }

}
